package com.xiaode.controller.app.tip;

import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.xiaode.common.constant.Const;
import com.xiaode.common.model.*;
import com.xiaode.exce.EnumError;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


public class TipService {
	

	/**
	 * 获取提示信息审核提示,评价提示
	 * @return
	 * @throws Exception
	 */
	public Ret msgTips(Integer accountId, Integer adminId, Integer userType) throws Exception{
		if(userType != Const.UserType.admin) {
			return Ret.fail().set("error", EnumError.USER_TYPE_NO.getObj());
		}
		Cache redis = Redis.use();
		List<Integer> shbydss = new ArrayList<Integer>();
		List<Integer> shbyrwdss = new ArrayList<Integer>();
		List<Record> records = new ArrayList<>();
		List<TeacherStudent> tsList = TeacherStudent.dao.find("SELECT ts.id,ts.studentId FROM teacher_student ts INNER JOIN student stu ON ts.studentId = stu.id AND stu.isDele = 0 WHERE ts.teacherId = ? AND ts.state = 2 ", adminId);
		List<Integer> stuAccountIdList = tsList.stream().map(teacherStudent -> ((Student)redis.get(Const.user.Student + teacherStudent.getStudentId())).getAccountId()).collect(Collectors.toList());
		if (!stuAccountIdList.isEmpty()) {
			Kv kv = Kv.by("stuAccountIdList", stuAccountIdList);
			records = Db.template("findTaskLogSta", kv).find();
		}
		for(TeacherStudent ts : tsList) {
			//审核的榜样1导师(app端只是绑定的)
			List<Integer> shbyds = Db.query("SELECT id FROM example WHERE studentId = ? AND state = 1 AND isDele = 0", ts.getStudentId());
			shbydss.addAll(shbyds);
			//审核的榜样任务
			List<Integer> shbyrwds = Db.query("SELECT id FROM student_example_task WHERE studentId = ? AND state = 1 ", ts.getStudentId());
			shbyrwdss.addAll(shbyrwds);

		}
		//申请绑定
		List<Integer> bdxs = Db.query("SELECT id FROM teacher_student WHERE teacherId = ? AND state = 1", adminId);
		//申请解绑
		List<Integer> jbxs = Db.query("SELECT id FROM teacher_student_log WHERE teacherId = ? AND state = 1 AND type = 2", adminId);

		//查询待审核活动
		List<Integer> activityIdList = Activity.dao.find("SELECT id FROM activity WHERE publisher_account_id = ?", accountId).stream().map(Activity::getId).collect(Collectors.toList());
		//根据活动id查询待审核的活动提交数量
		List<Record> list = new ArrayList<>();
		if (!activityIdList.isEmpty()){
			list = Db.template("findUserActivityByActivityIdList", Kv.by("activityIdList", activityIdList).set("state",1)).find();
		}
		return Ret.ok().set("shbydss", shbydss).set("shbyrwdss", shbyrwdss).set("bdxs", bdxs).set("jbxs",jbxs).set("unExaminTask",records).set("unExaminActivity",list);
	}
	
	


}
